package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.patrons.PatronsBreak;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 *  充值vip登陸活动（从注册到当前有效）
 */
public class FirstRechargeVipDayCondition extends BaseActivityCondition {

	public FirstRechargeVipDayCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.HistoryRechargeVipExpDay.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.HistoryRechargeVipExpDay.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		List<Integer> paramList = StringUtils.stringToIntegerList(getParam(),"\\|");
		int param1 = 0;
		int param2 = 0;
		if(paramList.size() > 1){
			param1 = paramList.get(0);
			param2 = paramList.get(1);
		}
		return BigInteger.valueOf(param1).compareTo(activityConditionInfo.getParamList().get(2)) >= 0
				&& BigInteger.valueOf(param2).compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		long vipExp = player.getUserInfo().getVipExp();
		long day = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.LoginTotalDays);
		setParam(vipExp + "|" + day);
		setValue(BigInteger.ZERO);//同步用
	}

	@Override
	public void initRechargeConditionData() {
		player.getModule(EarnSpeedModule.class).resetDay();
		initParam();
	}

	private void initParam(){
		if ("".equals(getParam())) {
			changeValue(null);
		}
	}

	@Override
	public String getReward() {
		String reward = super.getReward();

		try{
			//首充、镖师
			if(this.activityInfo.getType() == eActivityType.RechargeActivity.getValue() && this.activityInfo.getChildType() == 16){
				Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(this.activityInfo.getActivityId());
				if(activityConfigMap != null && activityConfigMap.containsKey(eActivityConfigType.RECHARGE_REWARD_PATRONS_LEVEL.getName())){
					List<Integer> paramList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.RECHARGE_REWARD_PATRONS_LEVEL.getName()).getValue(), "\\|");
					int patronsId = paramList.get(0);
					int level = paramList.get(1);
					Property property = PropertyHelper.parseStringToProperty(reward);
					for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
						int itemId = entry.getKey();
						//奖励有该门客
						if(GoodsMgr.isPatronsGoods(itemId) && patronsId == itemId){
							PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(itemId);
							UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsInfo.getId());
							if(userPatrons != null){
								//门客等级
								if(level > userPatrons.getLevel()) {
									userPatrons.setLevel(level);
								}
								if(paramList.size() == 3 && paramList.get(2) == 1){
									//突破等级
									int breakLv = 0;
									for (int i = 0; i < 500; i++) {
										PatronsBreak patronsBreak = PatronsMgr.getPatronsBreak(i);
										if(patronsBreak == null){
											continue;
										}
										if(level >= patronsBreak.getCurrentMaxLv()){
											breakLv = i;
										}
									}
									if(breakLv > 0){
										userPatrons.setBreakLv(breakLv);
									}
								}
								player.getModule(PatronsModule.class).syncPatronsData(userPatrons);

								player.getModule(PatronsModule.class).refreshCopyMaxPatronsLevel(false);
								break;
							}
						}
					}
				}
			}
		}catch (Exception e){
			logger.info("activity {} player {} recharge reward {} special handle error", this.activityInfo.getActivityId(), player.getUserId(), reward);
		}
		return reward;
	}

}
